package com.example.bluetooth.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.bluetooth.MyApp;
import com.example.bluetooth.R;
import com.example.bluetooth.retrofit.Api;
import com.example.bluetooth.retrofit.Result;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RegActivity extends AppCompatActivity {

    private Context context;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);
        setTitle("注册");


        Button btnReg = findViewById(R.id.btn_reg);
        EditText etName = findViewById(R.id.et_name);
        EditText etPwd = findViewById(R.id.et_pwd);
        EditText etPwd2 = findViewById(R.id.et_pwd2);
        TextView tvGoLogin = findViewById(R.id.tv_go_login);
        context = this;

        btnReg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String pwd1 = etPwd.getText().toString();
                String pwd2 = etPwd2.getText().toString();
                if (!pwd1.equals(pwd2)) {
                    Toast.makeText(context, "两次密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                }

                Map<String, String> map = new HashMap<>(5);
                map.put("name", etName.getText().toString());
                map.put("password", etPwd.getText().toString());
                map.put("nick", "正常用户");
                RequestBody body = FormBody.create(MediaType.parse("application/json; charset=utf-8"), new Gson().toJson(map));
                //接口对象实例调用相关接口，获得Observable对象
                Api api = MyApp.retrofit.create(Api.class);
                Call<Result> login = api.reg(body);
                login.enqueue(new Callback<Result>() {
                    @Override
                    public void onResponse(Call<Result> call, Response<Result> response) {
                        Result res = response.body();
                        if (res.getCode() == 20001) {
                            Toast.makeText(context, res.getMsg(), Toast.LENGTH_SHORT).show();
                        } else if (res.getCode() == 10001) {
                            Toast.makeText(context, "注册成功", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(context, LoginActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    }

                    @Override
                    public void onFailure(Call<Result> call, Throwable t) {
                        Toast.makeText(context, "注册失败，请检查网络", Toast.LENGTH_SHORT).show();
                    }
                });

            }
        });

        tvGoLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }
}
